#!/bin/bash

port1=$(($1*1000+10))
port2=$(($1*1000+11))
port3=$(($1*1000+12))
port4=$(($1*1000+13))
port5=$(($1*1000+14))
port6=$(($1*1000+15))
port7=$(($1*1000+16))
port8=$(($1*1000+17))
port9=$(($1*1000+18))
port10=$(($1*1000+19))


    #removing unecessary .o
#    make clear
    rm out*

    #Give permission to execute
    chmod u+x *script1.sh

    #running in background each node using ssh
    ./nivel4.sh $port8 $port7 > outlast.txt &
        sleep 2
    echo "Node 17"
    ssh node17 "./nivel3.sh frontend $port7 $port5 > out017.txt > out017.txt ; exit" &
    sleep 2
    echo "Node 16"
    ssh node16 "./nivel3.sh frontend $port7 $port6 > out016.txt > out016.txt ; exit" &
    sleep 2

    echo "Node 15"
    ssh node15 "./nivel2.sh node17 $port5 $port1 > out015.txt ; exit" &
    sleep 2
    echo "Node 12"
    ssh node12 "./nivel2.sh node17 $port5 $port2 > out012.txt ; exit" &
    echo "Node 11"
    sleep 2
    ssh node11 "./nivel2.sh node16 $port6 $port3 > out011.txt ; exit" &
    sleep 2
    echo "Node 10"
    ssh node10 "./nivel2.sh node16 $port6 $port4 > out010.txt ; exit" &
    sleep 2

    echo "Node 1"
    ssh node1 "./nivel1.sh node10 $port4 $port8 > out001.txt ; exit" &
    sleep 2
	echo "Node 9"
    ssh node9 "./nivel1.sh node10 $port4 $port8 > out009.txt ; exit" &
    sleep 2
    echo "Node 3"
    ssh node3 "./nivel1.sh node11 $port3 $port8 > out003.txt ; exit" &
    sleep 2
    echo "Node 4"
    ssh node4 "./nivel1.sh node11 $port3 $port8 > out004.txt ; exit" &
    sleep 2
    echo "Node 5"
    ssh node5 "./nivel1.sh node12 $port2 $port8 > out005.txt ; exit" &
    sleep 2
    echo "Node 6"
    ssh node6 "./nivel1.sh node12 $port2 $port8 > out006.txt ; exit" &
        sleep 2
        echo "Node 7"
    ssh node7 "./nivel1.sh node15 $port1 $port8 > out007.txt ; exit" &
    sleep 2
    echo "Node 8"
    ssh node8 "./nivel1.sh node15 $port1 $port8 > out008.txt ; exit" &
    sleep 2

#fi
